﻿using BootyBay.PrivateStoreApp.AgentServicesRef;
using BootyBay.PrivateStoreApp.Common;
using BootyBay.PrivateStoreApp.DataModel;
using BootyBay.PrivateStoreApp.StoreServicesRef;
using BootyBay.PrivateStoreApp.ViewModels;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Management.Deployment;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

//“中心页”项模板在 http://go.microsoft.com/fwlink/?LinkId=321224 上有介绍

namespace BootyBay.PrivateStoreApp
{
    /// <summary>
    /// 显示分组的项集合的页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private DispatcherTimer timer = new DispatcherTimer();
        private bool IsCanEnterSearch = false;
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();
        /// <summary>
        /// 可将其更改为强类型视图模型。
        /// </summary>
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }

        /// <summary>
        /// NavigationHelper 在每页上用于协助导航和
        /// 进程生命期管理
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        public MainPage()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;

            if (!AppListPageVM.Instance.IsLoadingInfo)
            {
                IsLoadInfo(true);
            }
        }

        /// <summary>
        /// 使用在导航过程中传递的内容填充页。  在从以前的会话
        /// 重新创建页时，也会提供任何已保存状态。
        /// </summary>
        /// <param name="sender">
        /// 事件的来源; 通常为 <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">事件数据，其中既提供在最初请求此页时传递给
        /// <see cref="Frame.Navigate(Type, Object)"/> 的导航参数，又提供
        /// 此页在以前会话期间保留的状态的
        /// 字典。 首次访问页面时，该状态将为 null。</param>
        private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            this.DefaultViewModel["applistPageVM"] = AppListPageVM.Instance;
            this.DefaultViewModel["appCategoriesPageVM"] = AppCategoriesPageVM.Instance;
        }

        #region NavigationHelper 注册

        /// 此部分中提供的方法只是用于使
        /// NavigationHelper 可响应页面的导航方法。
        /// 
        /// 应将页面特有的逻辑放入用于
        /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
        /// 和 <see cref="GridCS.Common.NavigationHelper.SaveState"/> 的事件处理程序中。
        /// 除了在会话期间保留的页面状态之外
        /// LoadState 方法中还提供导航参数。

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
        }

        #endregion

        #region 是否显示加载
        private void IsLoadInfo(bool bo)
        {
            if (bo)
            {
                progressRing_LoadInfo.IsActive = true;
                this.Opacity = 0.8;
                this.IsHitTestVisible = false;

                timer.Interval = new TimeSpan(0, 0, 1);
                timer.Tick += timer_Tick;
                timer.Start();
            }
            else
            {
                timer.Stop();
                progressRing_LoadInfo.IsActive = false;
                this.Opacity = 1;
                this.IsHitTestVisible = true;
            }
        }
        void timer_Tick(object sender, object e)
        {
            if (AppListPageVM.Instance.IsLoadingInfo)
            {
                IsLoadInfo(false);
            }
        }
        #endregion

        #region 点击列表项转到详细页
        private async void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            UserAppDataModel items = (UserAppDataModel)e.ClickedItem;
            if (items.AppView.IsPublicApp)
            {
                if (items.AppView.PublisherDisplayName != "")
                {
                    AppListPageVM.Instance.OpenPublicAppDetailPage(items.AppView.Publisher);
                }
            }
            else
            {
                this.Frame.Navigate(typeof(ItemDetailPage), items);
            }
        }
        #endregion

        #region 转到你的应用页
        private void btn_AppList_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(YourAppsPage));
        }
        #endregion

        #region 转到主页
        private void btn_Home_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }
        #endregion

        #region 转到推荐页
        private void Hub_SectionHeaderClick(object sender, HubSectionHeaderClickEventArgs e)
        {
            if (e.Section.Header.ToString() == "推荐")
            {
                Category category = new Category();
                this.Frame.Navigate(typeof(AppCategoriesPage), category);
            }
        }
        #endregion

        #region 搜索框获取焦点时触发
        private void txtSearch_GotFocus(object sender, RoutedEventArgs e)
        {
            IsCanEnterSearch = true;
            if (txtSearch.Text == "搜索应用")
            {
                txtSearch.Text = "";
            }
        }
        #endregion

        #region 搜索框失去焦点时触发
        private void txtSearch_LostFocus(object sender, RoutedEventArgs e)
        {
            IsCanEnterSearch = false;
            if (txtSearch.Text == "")
            {
                txtSearch.Text = "搜索应用";
            }
        }
        #endregion

        #region 单击搜索按钮
        private void btn_Search_Click(object sender, RoutedEventArgs e)
        {
            if (txtSearch.Text != "搜索应用")
            {
                this.Frame.Navigate(typeof(SearchResultsPage), txtSearch.Text);
            }
        }
        #endregion

        #region 按下回车按键执行搜索
        private void pageRoot_KeyDown(object sender, KeyRoutedEventArgs e)
        {
            if (IsCanEnterSearch && e.Key.ToString() == "Enter")
            {
                if (txtSearch.Text != "搜索应用" && txtSearch.Text.Trim() != "")
                {
                    this.Frame.Navigate(typeof(SearchResultsPage), txtSearch.Text);
                }
            }
        }
        #endregion

        #region 跳转到指定类别应用类别
        private void ItemListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Frame.Navigate(typeof(AppCategoriesPage), e.ClickedItem);
        }
        #endregion
    }
}
